// 官方文档：https://vue3js.cn/router4/guide/#html
// 引入vue-router对象
// createWebHashHistory 是hash模式就是访问链接带有#
// createWebHistory  是history模式

import { createRouter, createWebHistory } from "vue-router";
// 引入路由守卫方法
import beforeEach from "/@/router/beforeEach";

/**
 * 定义路由数组
 */
const routes = [
  {// 404路由
    path: '/404',
    name: '404',
    meta: {
      requireAuth: true // false需要登录, true 不需要登录
    },
    component: ()=>import('/@/views/error/404.vue')// 懒加载组件
  },
  {// 后端管理系统路由组
    path: "/admin",
    redirect: "/admin/home",
    name: "admin",
    component: () => import("/@/views/admin.vue"),
    children: [
      {
        path: "login",
        name: "login",
        meta: {
          requireAuth: true // false需要登录, true 不需要登录
        },
        component: () => import("/@/views/admin/login.vue")
      },
      {
        path: "home",
        name: "home",
        component: () => import("/@/views/admin/home.vue")
      },
    ]
  },
  {// 博客主站pc端页面路由
    path: "/pc",
    // redirect: "/pc/index/1/search",
    redirect: "/pc/index",
    name: "pc",
    component: () => import("/@/views/pc.vue"),
    children: [
      {
        // path: "index/:page/:search",
        path: "index",
        name: "首页",
        meta: {
          requireAuth: true // false需要登录, true 不需要登录
        },
        component: () => import("/@/views/pc/index.vue"),
      }
    ]
  },
  {// 博客主站手机端页面路由
    path: "/phone",
    redirect: "/phone/index",
    name: "phone",
    component: () => import("/@/views/phone.vue"),
    children: [
      {
        path: "index",
        name: "index",
        meta: {
          requireAuth: true // false需要登录, true 不需要登录
        },
        component: () => import("/@/views/phone/index.vue")
      },
    ]
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  // hash模式：createWebHashHistory，
  // history模式：createWebHistory
  history: createWebHistory("/"),
  // history:createWebHashHistory("/#"),
  routes,
});

/**
 * 路由守卫
 */
router.beforeEach((guard) => {
   //用于判断该路由的访问是否需要登录
  // requireAuth为true时，不需要登录
  // 不设置requireAuth为true时，需要登录
	beforeEach.checkAuth(guard, router);
});

/**
 * 路由错误回调
 */

router.onError((callback) => {
  console.log(callback)
});
/**
 * 输出对象
 */
export default router;
